//
//  NWNavigationViewController.swift
//  Weibo
//
//  Created by Northwan on 16/1/20.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWNavigationController: UINavigationController ,UIGestureRecognizerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        interactivePopGestureRecognizer?.delegate = self
    }
    
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
        if childViewControllers.count > 0{
            //如果是第2级界面就显示上个界面的title，如果是3级界面以上就显示'返回'
            var title: String = "返回"
            
            if childViewControllers.count == 1{
                //说明是第2级界面
                title = childViewControllers.first?.title ?? title
            }
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext",title: title ,target: self, action: "back")
            
            //二级界面隐藏tabar
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func back(){
        popViewControllerAnimated(true)
    }
    
    // MARK: - UIGestureRecognizerDelegate
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        //如果是根控制器就不识别手势
        return childViewControllers.count > 1
    }

}
